# -*- coding=utf8 -*-
from mysqlmgr import mysqlmgr
from items import PospalMemberItem
import logging

def process_item(item):
    if item is None:
        return
    # 没有手机号的不处理
    if item['mobile'] is None or item['mobile'] == '':
        return
    deal_member(item)


def deal_member(item):
    # 保存当前表,废弃
    # if not is_exist_in_current_pospal(item['member_number']):
    #     with mysqlmgr.mysql() as cursor:
    #         insert_sql = 'insert into pospal_member(member_number, real_name, mobile, member_point, is_enabled, is_dealed, create_time, modify_time)values(%s, %s, %s, %s, %s, %s, %s, %s)'
    #         params = (
    #         item['member_number'], item['real_name'], item['mobile'], item['member_point'], item['is_enabled'],
    #         item['is_dealed'], item['create_time'], item['modify_time'])
    #         cursor.execute(insert_sql, params)

    # 保存历史表
    exist_item = is_exist_in_history_pospal(item['member_number'], item['member_point'])
    if exist_item is None: #不存在
        with mysqlmgr.mysql() as cursor:
            insert_history_sql = 'insert into pospal_member_history(member_number, real_name, mobile, member_point, is_enabled, is_dealed, create_time, modify_time)values(%s, %s, %s, %s, %s, %s, %s, %s)'
            params = (
            item['member_number'], item['real_name'], item['mobile'], item['member_point'], item['is_enabled'],
            item['is_dealed'], item['create_time'], item['modify_time'])
            cursor.execute(insert_history_sql, params)

            # 写入task表，类型为1:add_member
            change_point = 0
            insert_task_sql = 'insert into pospal_member_task(member_number, real_name, mobile, old_point, new_point, change_point, task_type, is_dealed, create_time, modify_time)values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
            params = (
            item['member_number'], item['real_name'], item['mobile'],  0, item['member_point'], change_point, 1,
            item['is_dealed'], item['create_time'], item['modify_time'])
            cursor.execute(insert_task_sql, params)
    else:# 存在
        with mysqlmgr.mysql() as cursor:
            if int(item['member_point']) == int(exist_item['member_point']):
            # 积分相同,only update modify_time
                modify_sql = 'update pospal_member_history set modify_time= %s WHERE id=%s '
                params = (item['modify_time'], exist_item['id'])
                cursor.execute(modify_sql, params)
                logging.debug('update pospal hisotry')
                print('update pospal hisotry', exist_item['mobile'])
            else:
                # 积分不同，写入task表，类型为2:add_point
                insert_history_sql = 'insert into pospal_member_history(member_number, real_name, mobile, member_point, is_enabled, is_dealed, create_time, modify_time)values(%s, %s, %s, %s, %s, %s, %s, %s)'
                params = (
                    item['member_number'], item['real_name'], item['mobile'], item['member_point'],
                    item['is_enabled'],item['is_dealed'], item['create_time'], item['modify_time'])
                cursor.execute(insert_history_sql, params)

                change_point = int(item['member_point']) - int(exist_item['member_point'])
                insert_task_sql = 'insert into pospal_member_task(member_number, real_name, mobile, old_point, new_point, change_point, task_type, is_dealed, create_time, modify_time)values(%s, %s, %s, %s, %s, %s, %s, %s,%s, %s)'
                params = (
                item['member_number'], item['real_name'], item['mobile'], exist_item['member_point'],item['member_point'], change_point,2,
                item['is_dealed'], item['create_time'], item['modify_time'])
                cursor.execute(insert_task_sql, params)

def is_exist_in_current_pospal(member_number):
    with mysqlmgr.mysql() as cursor:
        select_sql = 'select id, member_number, real_name, mobile, member_point from pospal_member where member_number=%s'
        params = member_number
        count = cursor.execute(select_sql, params)
    return count != 0  # true exist

#
# 根据member_number+point判断历史积分表是否存在
def is_exist_in_history_pospal(member_number, member_point):
    with mysqlmgr.mysql() as cursor:
        select_sql = 'select id, member_number, real_name, mobile, member_point,is_enabled, is_dealed, create_time, modify_time from pospal_member_history where member_number=%s order by modify_time desc limit 0,1'
        params = member_number
        cursor.execute(select_sql, params)
        row = cursor.fetchone()
        if row is None:
            return None
        else:
            item = PospalMemberItem()
            item['id'] = row['id']
            item['member_number'] = row['member_number']
            item['real_name'] = row['real_name']
            item['mobile'] = row['mobile']
            item['member_point'] = row['member_point']
            item['is_enabled'] = row['is_enabled']
            item['is_dealed'] = row['is_dealed']
            item['create_time'] = row['create_time']
            item['modify_time'] = row['modify_time']
            return item


def get_from_history_by_mobile(mobile):
    with mysqlmgr.mysql() as cursor:
        select_sql = 'select id, member_number, real_name, mobile, member_point,is_enabled, is_dealed, create_time, modify_time from pospal_member_history where mobile=%s order by modify_time desc limit 0,1'
        params = mobile
        cursor.execute(select_sql, params)
        row = cursor.fetchone()
        if row is None:
            return None
        else:
            item = PospalMemberItem()
            item['id'] = row['id']
            item['member_number'] = row['member_number']
            item['real_name'] = row['real_name']
            item['mobile'] = row['mobile']
            item['member_point'] = row['member_point']
            item['is_enabled'] = row['is_enabled']
            item['is_dealed'] = row['is_dealed']
            item['create_time'] = row['create_time']
            item['modify_time'] = row['modify_time']
            return item
